public class Test2 {
    public static void main(String[] args) {
        String s = "PAYPALISHIRING";
        convert(s,4);
    }
    public static String convert(String s, int numRows) {
        if(numRows == 1) return s;
        int len = s.length();
        char[] str = new char[len];
        int d = numRows*2-2,k=0;//计算公差
        //1.读入第一行
        for(int i=0;i<len;i=i+d) str[k++] = s.charAt(i);
        //2.读入中间行
        for(int i=1;i<numRows-1;i++) {//控制行
            int x=1,y=d-i;
            while (true) {
                if(x<len)
                    str[k++] = s.charAt(x);
                else break;
                if(y<len) {
                    str[k++] = s.charAt(y);
                }else break;
                x+=d;
                y+=d;
            }
        }
        //3.读入最后一行
        for(int i=numRows-1;i<len;i=i+d)
            str[k++] = s.charAt(i);
        return String.valueOf(str);

    }
}
